import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/10/31 20:40
 */
public class SelectJDBC {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("001110");

        Connection connection = dataSource.getConnection();

        String sql = "select * from student";

        PreparedStatement statement = connection.prepareStatement(sql);

        ResultSet resultSet = statement.executeQuery();
        /**
         * 遍历到表的结束为止，此处的 next 直接返回 false
         */
        while (resultSet.next()){
            //针对当前这一行来获取到其中的列
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            double score = resultSet.getDouble("score");
            System.out.println("id="+id+",name="+name+",age="+age+",score="+score);
        }


        resultSet.close();
        statement.close();
        connection.close();

    }
}
